package cn.zzszxyy.hr.controller;

import cn.zzszxyy.hr.comm.Global;
import cn.zzszxyy.hr.comm.StringUtil;
import cn.zzszxyy.hr.model.DepartmentEntity;
import cn.zzszxyy.hr.model.EmployeeEntity;
import cn.zzszxyy.hr.model.UserEntity;
import cn.zzszxyy.hr.service.DeptService;
import cn.zzszxyy.hr.service.DeptTransferService;
import cn.zzszxyy.hr.service.EmployeeService;
import cn.zzszxyy.hr.service.JobTitleService;
import cn.zzszxyy.hr.support.AuthPassport;
import cn.zzszxyy.hr.viewmodel.DeptTransView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 作者:张伟
 * <p>
 * 联系:adamzzww@163.com
 * 时间:2016-11-28
 * 说明:雇员信息
 */
@Controller
@RequestMapping(Global.PATH)
public class EmployeeController extends ControllerEx {

    private final String path = "/employee";

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private DeptTransferService deptTransferService;

    @Autowired
    private JobTitleService jobTitleService;

    @AuthPassport
    @RequestMapping(value = path + "/list", method = RequestMethod.GET)
    public String get(HttpServletRequest request, ExtendedModelMap map) {
        Integer pageIndex = getPageIndex(request);
        String employeeName = request.getParameter("employeeName");
        if (StringUtil.isEmpty(employeeName)) {
            Page<EmployeeEntity> page = employeeService.findAllWithPage(pageIndex, Global.PAGE_SIZE);
            map.put("page", page);
        } else {
            Page<EmployeeEntity> page = employeeService.findByEmployeeNameLike(employeeName, pageIndex, Global.PAGE_SIZE);
            map.put("page", page);
            map.put("employeeName", employeeName);
        }

        map.put("pageIndex", pageIndex);

        return Global.DESKTOP + path + "/employee-list";
    }

    @RequestMapping(value = path + "/moveDept/{id}", method = RequestMethod.GET)
    public String moveDept(@PathVariable int id, ExtendedModelMap map) {
        EmployeeEntity entity = employeeService.findOne(id);
        map.put("entity", entity);
        List<DepartmentEntity> deptList = deptService.findAll();
        map.put("deptList", deptList);
        return Global.DESKTOP + path + "/move-dept";
    }


    @RequestMapping(value = path + "/{id}", method = RequestMethod.GET)
    public String get(@PathVariable int id, ExtendedModelMap map) {
        EmployeeEntity entity = employeeService.findOne(id);
        map.put("entity", entity);

        List<DepartmentEntity> deptList = deptService.findAll();
        map.put("deptList", deptList);
        return Global.DESKTOP + path + "/employee-edit";
    }

    @RequestMapping(value = path + "/browse/{id}", method = RequestMethod.GET)
    public String browse(@PathVariable int id, ExtendedModelMap map) {
        EmployeeEntity entity = employeeService.findOne(id);
        map.put("entity", entity);
        String deptName = "";
        if (null != entity.getCurDept()) {
            deptName = deptService.findOne(entity.getCurDept()).getDeptName();
        }
        map.put("deptName", deptName);
//        List<DepartmentEntity> deptList = deptService.findAll();
//        map.put("deptList", deptList);
        List<DeptTransView> transList = deptTransferService.findByEmployeeId(id);
        map.put("transList", transList);
        return Global.DESKTOP + path + "/employee-info";
    }


    @RequestMapping(value = path + "/add", method = RequestMethod.GET)
    public String add(ExtendedModelMap map) {
        List<DepartmentEntity> deptList = deptService.findAll();
        map.put("deptList", deptList);
        return Global.DESKTOP + path + "/employee-edit";
    }

    @ResponseBody
    @RequestMapping(value = path + "/save", method = {RequestMethod.PUT, RequestMethod.POST})
    public String post(HttpServletRequest request, EmployeeEntity entity) {
        UserEntity user = (UserEntity) request.getSession().getAttribute(Global.COOKIE_USER_OBJECT);
        entity.setCreateTime(new Date());
        entity.setCreator(user.getUserName());
        if (employeeService.save(entity) != null) {
            return Global.RE_SUCCESS;
        } else {
            return Global.RE_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping(value = path + "/feedback", method = {RequestMethod.PUT, RequestMethod.POST})
    public String feedback(HttpServletRequest request, EmployeeEntity entity) {
        UserEntity user = (UserEntity) request.getSession().getAttribute(Global.COOKIE_USER_OBJECT);
        entity.setUpdateTime(new Date());
        entity.setUpdater(user.getUserName());
        if (null != entity.getEmployeeId()) {
            EmployeeEntity curEntity = employeeService.findOne(entity.getEmployeeId());
            curEntity.setRemark(entity.getRemark());
            if (employeeService.save(curEntity) != null) {
                return Global.RE_SUCCESS;
            } else {
                return Global.RE_ERROR;
            }
        } else {
            return Global.RE_ERROR;
        }

    }

    @ResponseBody
    @RequestMapping(value = path + "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable int id) {
        employeeService.delete(id);
        return Global.RE_SUCCESS;//"redirect:" + Global.Web.PATH + path;
    }

}
